<?php
//On defini un titre pour la page. Si null titre par defaut = Nom du site
setPageTitle("Forum - Accueil");
$display = ''."\n";
     
    /* Forum normal */
 
    // ,ff.time_last_reponse as time_last_reponse
    $sql = "SELECT * FROM forum_categorie AS fc
    JOIN forum_forum AS ff ON ff.forum_cat_id=fc.cat_id
    WHERE fc.cat_level<=?
    GROUP BY forum_id
    ORDER BY fc.cat_ordre ASC
     
    ";
    $req = $pdo->prepare($sql);
     
    //$req->bindParam(":level",id2level(),PDO::PARAM_INT);
        if(isset($_SESSION['id']))
            $req->execute(array(id2level($_SESSION['id'])));
        else
            $req->execute(array(1));
 
    $display .= "\n\t".'';
     			// ici on affiche un texte en hut de la page pour le changer c'esty dans modules/message_haut.php
echo '' . Forum_index . '<br />

         
    '."\n";
                 
    if($req->rowCount() < 1)
    {          
        $display .= "\t".' '."\n\t\t".' ' . display_info("Il n'y à aucun forum à afficher pour le moment !",WARNING,false) . ' '."\n\t".' ';
    }
     
    //Permet de stocké la catégorie en cours pour la comparer à chaque tour de boucle si on change ou pas de catégorie
    $cat = NULL;
 
    $j = 0;
 
    $display .= '';
     
    $nbForum = $req->rowCount();
        if($nbForum >= 0 && id2level() == ADMIN){
            if($nbForum == 0)
                $display .= "\t" .'Admin, vous lancez pour la premiere fois ce forum, par concequent vous devez <a href="' . getLienModule("forum","forum.admin") . '?action=form_addCat"><u>Creer une nouvelle catégorie</u></a> avant de pouvoir créer un forum'."\n";
            else
                $display .= "\t" .'<a href="' . getLienModule("forum","forum.admin") . '?action=form_addCat">Admin, cliquez ici pour  créer une nouvelle catégorie</a>'."\n";
        }
         
        if($nbForum > 0 && id2level() == ADMIN)
            $display .= "\t" .'<a href="' . getLienModule("forum","forum.admin") . '?action=form_addForum">Admin, cliquez ici pour ajouter un forum</a>'."\n";
         
    while($data = $req->fetch())
     
     
 
    {
         
//echo "<br/>--------------<br/>";
//var_dump($data);
 
 
        /* deprecated by zulad
        $req2 = $pdo->prepare("SELECT COUNT(*) AS nbSujet FROM forum_forum_view WHERE fv_forum_id=:id");
        $req2->bindParam(":id",$data["forum_id"],PDO::PARAM_INT);
        $req2->execute();
        $nb = $req2->fetch();
        $req2->closeCursor();
        */
 
        //Couleur de fond ce tableau paire ou impaire si $j%2 = 0 $jmodulo2 => $j reste de la division par 2. exemple 1/2 = 0.5 reste 1 = impaire , 2/2 = 1 reste 0 = paire
        $id = ($j%2 == 0) ? 'line_pair' : 'line_impair';
        $display .= "\n\t\t";
         
        $topic = $pdo->query("SELECT topic_id FROM forum_topic WHERE forum_id=".$data['forum_id']);
        $post = $pdo->query("SELECT post_id FROM forum_post WHERE post_forum_id=".$data['forum_id']);
        //var_dump($topicLine[$data['forum_id']]);
        if($cat != $data["forum_cat_id"])
        {
            /*$totalTopicCat = $pdo->query("SELECT fc.cat_id FROM forum_categorie AS fc
                                         JOIN forum_forum AS ff ON ff.forum_cat_id=fc.cat_id
                                         JOIN forum_topic AS ft ON ft.forum_id=ff.forum_id
                                         WHERE fc.cat_id=".$data["forum_cat_id"]);*/
            $totalTopicCat = $pdo->query("SELECT topic_id FROM forum_topic AS ft
                                         JOIN forum_forum AS ff ON ff.forum_id=ft.forum_id
                                         JOIN forum_categorie AS fc ON fc.cat_id=ff.forum_cat_id
                                         WHERE fc.cat_id=".$data["forum_cat_id"]);
                                          
            /*$totalPostCat = $pdo->query("SELECT fp.post_id FROM forum_categorie AS fc
                                         JOIN forum_forum AS ff ON ff.forum_cat_id=fc.cat_id
                                         JOIN forum_topic AS ft ON ft.forum_id=ff.forum_id
                                         JOIN forum_post AS fp ON fp.topic_id=ft.topic_id
                                         WHERE fc.cat_id=".$data["forum_cat_id"]." GROUP BY fp.topic_id");*/
             
            $totalPostCat = $pdo->query("SELECT post_id FROM forum_post AS fp
                                         LEFT JOIN forum_topic AS ft ON fp.topic_id=ft.topic_id
                                         LEFT JOIN forum_forum AS ff ON ff.forum_id=ft.forum_id
                                         JOIN forum_categorie AS fc ON fc.cat_id=ff.forum_cat_id
                                         WHERE fc.cat_id=".$data["forum_cat_id"]);
                
            if($cat != NULL)
             
            // si un jour je remets un coin arrondit bas cest là je mettrais entre guillemet
                $display .= '';
                 
            $display .= "\n\t".($cat==NULL?'':'</div>').'<div class="cadre_visible contenu_forum">';
            $display .= '<div class="titre_forum">'.htmlspecialchars($data['cat_nom']).''."\n\t".'
						 <span class="texte_droite1">'.$totalTopicCat->rowCount().' Sujets & '.$totalPostCat->rowCount().' Réponses.</span></div>';
            $display .= "\n\t".($cat==NULL?'':'').'';
 
                    $cat = $data["forum_cat_id"];
        }
        $data['forum_id'];
         
            $topic = $pdo->query("SELECT topic_id,topic_titre FROM forum_topic WHERE forum_id=".$data['forum_id']);
        $topicLine = $topic->fetch();
         
        if($topicLine['topic_id'])
        {
            $sql2 = "SELECT * FROM forum_post WHERE topic_id=".$topicLine['topic_id']." ORDER BY post_time DESC LIMIT 1";
            $sqll = $pdo->prepare($sql2);
            $sqll->execute();
             
            $q2 = $sqll->fetch();
        }
         
        if($topicLine['topic_id'])
        {
            $fvquery = 'SELECT * FROM forum_forum_view JOIN forum_post ON fv_post_id=post_id WHERE fv_membre_id=? AND fv_topic_id=? AND fv_post_id=?';
            $fv = $pdo->prepare($fvquery);
             
            if(isset($_SESSION['id']))
                $fv->execute(array($_SESSION['id'],$topicLine['topic_id'],$q2['post_id']));
            else
                $fv->execute(array(-1,$topicLine['topic_id'],$q2['post_id']));
             
            if(!$fv->rowCount())
                $syslu = '<img src="'.DIR_DESIGN.'/'.getDesignName().'/images/forum/non_lu.gif" title="Nouvelles réponses" alt="Nouvelles réponses" height="20"/>';
                else
                $syslu = '<img src="'.DIR_DESIGN.'/'.getDesignName().'/images/forum/lu.gif" title="Pas de nouvelles réponses"  alt="Pas de nouvelles réponses" height="20"/>';
        }
   	
            $display .= '<div class="hover_du_categorie_forum hover_du_categorie_noforum parente">';
		
		
		$display .= '<div class="index_systeme_lu_non_lu">'.$syslu.'</div>';
        $display .= '<div class="index_categorie">'."\t\t\t" . '<a href="' . getLienModule($_GET["m"],"voirtopic") .'?fid=' . intval($data["forum_id"]) . '&amp;num=1">' . $data['forum_name'] . '</a>
		             <br />'.$data['forum_desc'].'</div>';
        $display .= "\n\t\t".'<div class="index_rep_vue">' . $topic->rowCount() . ' Sujets<br />' . $post->rowCount() . ' réponses</div>';
 
          
         $query2 = 'SELECT topic_id,forum_membre_id FROM forum_post WHERE post_forum_id=? ORDER BY topic_id DESC';
          
            $r = $pdo->prepare($query2);
            $r->execute(array($data["forum_id"]));
             
            $data2 = $r->fetch();
             
            //$topici = $pdo->query("SELECT topic_id,topic_titre FROM forum_topic WHERE forum_id=".$data['forum_id']);
        //$topicLine2 = $topici->fetch();
         
 
        if(!$r->rowCount())
            $display .= "\n\t\t".'<div class="index_auteur">&nbsp;   &nbsp;</div><div class="index_dernier_message">N/A'."\n\t".'</div></div>'."\n\t".'' . "\n\t";
        else
        {
$display .= "\n\t\t" .'<div class="index_auteur">'.GetSuperMiniAvatarById( $data2 ["forum_membre_id"] ) .'</div>';				
$max1=100;

$lien = getLienModule('forum', 'voirmessage') . '?fid=' . $data['forum_id'] . '&amp;tid=' . $topicLine['topic_id'];
$titre = substr($topicLine ['topic_titre'], 0, $max1) . '...';
 
$display .= '<div class="index_dernier_message"><a href="' . $lien . '">' . $titre . '</a><br />'.(LastPostedBy($topicLine['topic_id'])).'</div></div>';
        }
        $j++;
               
              $r->closeCursor();
    }
     
 $display .= '</div>'; 
   // include "./modules/forum/legende/index.php";
    /*$display .= "\n".'<div class="contour_du_forum">
    &nbsp;
    Catégories
    Sujets
    Dernier message
    <br /></div>   ';*/
        
     
    $req->closeCursor();
    // si un jour je remets un coin arrondit bas cest là je mettrais entre guillemet
    print $display . '';
     
    // todo
    //include_once(DIR_MODULE."/forum/down.php");
     